# -*- coding:utf-8 -*-

import requests
from lxml import etree
from fake_useragent import UserAgent
from http import cookiejar
import json
from datetime import datetime
import time
import re
import csv
from utils.HttpUtils import HttpUtils


requests.packages.urllib3.disable_warnings()  # 忽略HTTPS安全警告


"""
把手案例-文书
http://www.lawsdata.com/
账号:18241255868
密码:gkx55868
文档下载地址 更改id即可
http://www.lawsdata.com/u/download/word?id=5df55f19ab936e4614984cd9
"""

class BaShou_Get():
    def __init__(self):
        #声明一个CookieJar对象实例来保存cookie
        # requests.utils.dict_from_cookiejar(html.cookies)  # 获取cookies转dict
        self.cookie = cookiejar.CookieJar()
        ua = UserAgent(use_cache_server=False)  # 禁用服务器缓存
        self.headers = {
            # "User-Agent": ua.random
            # 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.1 Safari/605.1.15'
            'Accept': 'application/json, text/javascript, */*; q=0.01',
            'Accept-Encoding': 'gzip, deflate',
            'Accept-Language': 'zh-CN,zh;q=0.9',
            'Connection': 'keep-alive',
            'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
            'Cookie': 'gr_user_id=fbc3c6bc-5c3e-469d-9a63-02844718d0c0; gr_session_id_9c57f1694d616c90=934c066b-c1ba-46fd-b90f-4038ace30dfd; Hm_lvt_e484b393f62b648c74a7d2bc1605083c=1584850086; gr_session_id_9c57f1694d616c90_934c066b-c1ba-46fd-b90f-4038ace30dfd=true; JSESSIONID=976DD3C4A78A801EAD892DCD63EC6839; Hm_lpvt_e484b393f62b648c74a7d2bc1605083c=1584851103',
            'DNT': '1',
            'Host': 'www.lawsdata.com',
            'Origin': 'http://www.lawsdata.com',
            'Referer': 'http://www.lawsdata.com/?q=eyJtIjoiYWR2YW5jZSIsImEiOnsiY2FzZVR5cGUiOlsiMSJdLCJwcm9jZWR1cmVJZCI6WyIxIl0sImluc3RydW1lbnRUeXBlSWQiOlsiMSJdLCJyZWFzb25JZCI6WyIwMDEiLCIwMDEwMDUwMDIiXSwicHJvdmluY2VJZCI6WyI3MzAwIl0sImNvdXJ0TGV2ZWwiOlsiNSIsIjYiXSwiY291cnRJZCI6WyI3MzAxIl0sImp1ZGdlbWVudFllYXIiOiIyMDE3LTIwMTkiLCJmdXp6eU1lYXN1cmUiOiIwIiwiY291cnQiOiLlm5vlt53nnIHmiJDpg73luILkuK3nuqfkurrmsJHms5XpmaLlj4rkuIvovpYiLCJyZWFzb24iOiLliJHkuovmoYjnlLEs55uX56qDIn0sInNtIjp7InRleHRTZWFyY2giOlsic2luZ2xlIl0sImxpdGlnYW50U2VhcmNoIjpbInBhcmFncmFwaCJdfX0=&s=',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36',
            'X-Requested-With': 'XMLHttpRequest'
        }


    def get_contents(self, cursor):
        postData = {
            # 'q': '{"caseType":["1"],"procedureId":["1"],"instrumentTypeId":["1"],"reasonId":["001","001005002"],"provinceId":["7300"],"courtLevel":["5","6"],"courtId":["7301"],"@judgementYear":{"begin":"2017","end":"2019"},"fuzzyMeasure":"0","pageNo":650,"sortField":"referencedType","sortOrder":"desc"}',
            'subLibraryId': '',

        }
        postData['q'] = '{"caseType":["1"],"procedureId":["1"],"instrumentTypeId":["1"],"reasonId":["001","001005002"],"provinceId":["7300"],"courtLevel":["5","6"],"courtId":["7301"],"@judgementYear":{"begin":"2017","end":"2019"},"fuzzyMeasure":"0","pageNo":%s,"sortField":"referencedType","sortOrder":"desc"}' % cursor
        url = "http://www.lawsdata.com/s/"
        html = HttpUtils.do_request("POST", url, self.headers, postData)

        data_json = json.loads(html.text)
        for d in data_json['globalObjects']['tweets']:
            dict_data = dict()

            print(str(dict_data))


if __name__ == '__main__':
    bashou = BaShou_Get()
    for i in range(1, 100):
        print(f"============{i}=============")
        bashou.get_contents(i)